<?php 
class ControllerModuleCompare extends Controller {
	protected function index() {
		if ($this->config->get('iconmodule_status')) {
			$this->data['iconmodule'] = html_entity_decode($this->config->get('iconmodule_code'), ENT_QUOTES);
		} else {
			$this->data['iconmodule'] = '';
		} 
		
		$this->language->load('module/compare');
		
		$this->load->model('tool/seo_url');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_empty'] = $this->language->get('text_empty');
		$this->data['text_remove'] = $this->language->get('text_remove');
		$this->data['text_confirm'] = $this->language->get('text_confirm');
		$this->data['text_compare'] = $this->language->get('text_compare');

		$this->data['compare'] = HTTP_SERVER . 'index.php?route=compare/compare';

		$this->data['products'] = array();
		
    	foreach ($this->compare->getProducts() as $result) {
        	$option_data = array();

        	foreach ($result['option'] as $option) {
          		$option_data[] = array(
            		'name'  => $option['name'],
            		'value' => $option['value']
          		);
        	}
			
      		$this->data['products'][] = array(
        		'key' 		 => $result['key'],
        		'name'       => $result['name'],
				'href'       => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']),
      		);
    	}

		$this->data['ajax'] = $this->config->get('compare_ajax');
		
		$this->id = 'compare';
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/compare.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/compare.tpl';
		} else {
			$this->template = 'default/template/module/compare.tpl';
		}
		
		$this->render();
	}
		
	public function callback() {
		$this->language->load('module/compare');

		$this->load->model('tool/seo_url');

		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			
			if (isset($this->request->post['action']) && $this->request->post['action'] == 'remove') {
          		$this->compare->remove($this->request->post['product_id']);
      		} else {
				if (isset($this->request->post['option'])) {
					$option = $this->request->post['option'];
				} else {
					$option = array();	
				}
				
      			$this->compare->add($this->request->post['product_id'], $option);
			}
		}
				
		$output = '<table cellpadding="2" cellspacing="0" style="width: 100%;">';
		
		if ($this->compare->getProducts()) {
		
    		foreach ($this->compare->getProducts() as $product) {
      			$output .= '<tr>';
        		$output .= '<td width="1" valign="top" align="left"><span class="compare_remove" id="' . $product['key'] . '" />&nbsp;</span></td><td width="1" valign="top" align="right">' .'&nbsp;</td>';
        		$output .= '<td align="left" valign="top"><a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $product['product_id']) . '">' . $product['name'] . '</a>';
          		$output .= '<div>';
	            
				foreach ($product['option'] as $option) {
            		$output .= ' - <small style="color: #999;">' . $option['name'] . ' ' . $option['value'] . '</small><br />';
	            }
				
				$output .= '</div></td>';
				$output .= '</tr>';
      		}
			
			$output .= '</table>';
    		$output .= '<br />';

			$view = HTTP_SERVER . 'index.php?route=compare/compare';
      		$output .= '<div style="padding-top:5px;text-align:center;clear:both;"><a href="' . $view . '">' . $this->language->get('text_compare') . '</a></div>';
		} else {
			$output .= '<div style="text-align: center;">' . $this->language->get('text_empty') . '</div>';
		}
		
		$this->response->setOutput($output, $this->config->get('config_compression'));
	} 	
}
?>